<template>
  <basic-container>
    <el-row :gutter="20">
      <el-col :span="4">
        <avue-tree :option="treeoption" :data="treedata" @node-click="nodeClick">
        </avue-tree>
      </el-col>
      <el-col :span="20">
        <avue-crud :option="option" :data="data" ref="crud" v-model="form" :table-loading="loading"
          :before-open="beforeOpen" @row-del="rowDel" @row-update="rowUpdate" @row-save="rowSave" @search-change="searchChange"
          @search-reset="searchReset" @on-load="onLoad">
          <template slot-scope="{row}" slot="status">
            <el-tag :type='row.status=="1"?"":"info"'>{{row.status=="1"?"启用":"停用"}}</el-tag>
          </template>
        </avue-crud>
      </el-col>
    </el-row>
  </basic-container>
</template>

<script>
  import {
    getList,
    remove,
    update,
    add,
    getDept,
    getDeptTree
  } from "@/api/system/dept";
  //import website from '@/config/website';

  export default {
    data() {
      return {
        loading: true,
        query: {
          parentId: ''
        },
        treedata: [],
        treeoption: {
          size: 'small',
          props: {
            label: 'name',
            value: 'deptNo'
          },
          menu: false,
          addBtn: false
        },
        form: {
          parentId: ''
        },
        selectionList: [],
        option: {
          searchMenuSpan: 4,
          tip: false,
          tree: true,
          border: true,
          index: true,
          defaultExpandAll: true,
          menuWidth: 150,
          viewBtn: false,
          column: [{
              label: "部门名称",
              prop: "name",
              searchPlaceholder: '编号&&名称',
              search: true,
              rules: [{
                required: true,
                message: "请输入部门名称",
                trigger: "blur"
              }]
            },
            {
              label: "部门编号",
              prop: "deptNo",
              width: "100px",
              rules: [{
                required: true,
                message: "请输入部门编号",
                trigger: "blur"
              }]
            },
            {
              label: "上级部门",
              prop: "parentId",
              dicData: [],
              type: "tree",
              hide: true,
              rules: [{
                required: false,
                message: "请选择上级部门",
                trigger: "click"
              }]
            },
            {
              label: "排序",
              prop: "sort",
              width: "60px",
              type: "number",
              rules: [{
                required: true,
                message: "请输入排序",
                trigger: "blur"
              }]
            },
            {
              label: "状态",
              prop: "status",
              type: "switch",
              hide: true,
              slot: true,
              width: "70px",
              value: true,
              dicData: [{
                  label: "停用",
                  value: false
                },
                {
                  label: "启用",
                  value: true
                }
              ]
            },
            {
              label: "备注",
              prop: "remark",
              span: 24,
              rules: [{
                required: false,
                message: "请输入备注",
                trigger: "blur"
              }]
            }
          ]
        },
        data: []
      };
    },
    computed: {
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    created() {
      getDeptTree().then(res => {
        this.findObject(this.option.column,'parentId').dicData = res.data.data;
      });
      getList({
        limit: 3
      }).then(res => {
        this.treedata = res.data.data;
      });
    },
    methods: {
      nodeClick(data) {
        this.query.parentId = data.id
        this.onLoad(this.query)
      },
      rowSave(row, loading, done) {
        add(row).then(() => {
          loading();
          this.onLoad(this.query);
        }, error => {
          done();
          console.log(error);
        });
      },
      rowUpdate(row, index, loading, done) {
        update(row).then(() => {
          loading();
          this.onLoad(this.query);
        }, error => {
          done();
          console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.query);
          });
      },
      searchReset() {
        this.onLoad();
      },
      searchChange(params, done) {
        this.onLoad(params);
        done();
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDept(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        if (["add"].includes(type)) {
          this.form.parentId = this.query.parentId;
        }
        done();
      },
      onLoad(params = {}) {
        getList(params).then(res => {
          this.data = res.data.data;
          this.loading = false;
        });
      }
    }
  };
</script>

<style>
</style>
